package transport

import (
	"context"
	"dawncloud/types"
	"encoding/json"
	"net/http"

	"github.com/gin-gonic/gin"
	"github.com/go-kit/kit/endpoint"
	transportHttp "github.com/go-kit/kit/transport/http"
)

func decodeHomeRequest(_ context.Context, r *http.Request) (interface{}, error) {
	var request types.HomeRequest
	if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
		return nil, err
	}
	return request, nil
}

func encodeHomeResponse(_ context.Context, w http.ResponseWriter, response interface{}) error {
	return json.NewEncoder(w).Encode(response)
}

func MakeHttphandler(endpoint endpoint.Endpoint) http.Handler {
	r := gin.Default()

	homeHandler := transportHttp.NewServer(
		endpoint,
		decodeHomeRequest,
		encodeHomeResponse,
	)

	r.GET("/sewage-data", func(c *gin.Context) {
		homeHandler.ServeHTTP(c.Writer, c.Request)
	})

	return r
}
